

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class PetServlet1
 */
public class PetServlet extends HttpServlet {
	 private String recommendedPet(int weight, int legs) { 
	        if (legs ==0) return "a goldfish"; 
	        if (legs ==4) { 
	           if (weight<20) return "a cat"; 
	           if (weight<100) return "a dog"; 
	        } 
	        return "a house plant"; 
	    } 

	    public void doPost(HttpServletRequest req, 
	                       HttpServletResponse resp ) 
	        throws ServletException, IOException { 

	        // get the input field values 
	        int petWeight = 0, petLegs = 0; 
	        try { 
	          petWeight = Integer.parseInt(req.getParameter("weight")); 
	          petLegs = Integer.parseInt(req.getParameter("legs")); 
	        } catch (NumberFormatException nfe) { 
	          petWeight=petLegs=-1; // indicates that we got an invalid number
	        } 
	        req.setAttribute("petWeight", petWeight);
	        req.setAttribute("petLegs", petLegs);
	        String recommendedPet=this.recommendedPet(petWeight, petLegs);
	        req.setAttribute("recommendedPet", recommendedPet);
	    	RequestDispatcher requestDispatcher = req.getRequestDispatcher("showpet.jsp");
			requestDispatcher.forward(req, resp);
	        

	

	      
	    } 
	} 
